package com.sun.common.core.utils;

import com.sun.common.core.excel.model.BasicConstant;
import org.apache.commons.collections4.CollectionUtils;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p> 公共工具
 *
 * @author luxiaojin
 * @version 1.0.0
 * @date 2023/1/12 15:41
 **/
public class ComUtils {


    /**
     * 将long类型的数据转换为二进制,左边自动补零
     *
     * @param number
     * @param length 位数长度，如果转换后的长度不够，左边自动补零
     * @return
     */
    public static String toBinaryLeftZero(long number, int length) {
        String s = Long.toBinaryString(number);
        StringBuilder sB = new StringBuilder();
        int diffLength = length - s.length();
        for (int i = 0; i < diffLength; i++) {
            sB.append("0");
        }
        sB.append(s);
        return sB.toString();
    }

    public static <T> List<T> page(long pageNo, long pageSize, final Collection<T> collection) {
        if (CollectionUtils.isEmpty(collection)) {
            return Collections.emptyList();
        }
        if (pageNo <= 0) {
            pageNo = BasicConstant.PAGE_NO;
        }
        if (pageSize < 1) {
            pageSize = BasicConstant.PAGE_SIZE;
        }
        return collection.stream().skip((pageNo - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
    }

}
